package com.hzlx.mesMaterialBatches.controller;

import java.util.List;
import java.io.IOException;

import com.hzlx.MesMaterialBatchesDto;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzlx.mesMaterialBatches.domain.MesMaterialBatches;
import com.hzlx.mesMaterialBatches.service.IMesMaterialBatchesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * MES系统物料批次，用于管理物料批次全流程信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/inventory")
public class MesMaterialBatchesController extends BaseController
{
    @Autowired
    private IMesMaterialBatchesService mesMaterialBatchesService;

    /**
     * 查询MES系统物料批次，用于管理物料批次全流程信息列表
     */
    @RequiresPermissions("material-service:batches:list")
    @GetMapping("/batches/list")
    public TableDataInfo list(MesMaterialBatches mesMaterialBatches)
    {
        startPage();
        List<MesMaterialBatches> list = mesMaterialBatchesService.selectMesMaterialBatchesList(mesMaterialBatches);
        return getDataTable(list);
    }

    /**
     * 导出MES系统物料批次，用于管理物料批次全流程信息列表
     */
    @RequiresPermissions("material-service:batches:export")
    @Log(title = "MES系统物料批次，用于管理物料批次全流程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/batches/export")
    public void export(HttpServletResponse response, MesMaterialBatches mesMaterialBatches)
    {
        List<MesMaterialBatches> list = mesMaterialBatchesService.selectMesMaterialBatchesList(mesMaterialBatches);
        ExcelUtil<MesMaterialBatches> util = new ExcelUtil<MesMaterialBatches>(MesMaterialBatches.class);
        util.exportExcel(response, list, "MES系统物料批次，用于管理物料批次全流程信息数据");
    }

    /**
     * 获取MES系统物料批次，用于管理物料批次全流程信息详细信息
     */
    @RequiresPermissions("material-service:batches:query")
    @GetMapping(value = "/batches/{batchId}")
    public AjaxResult getInfo(@PathVariable("batchId") Long batchId)
    {
        return success(mesMaterialBatchesService.selectMesMaterialBatchesByBatchId(batchId));
    }
    @GetMapping(value = "/batchesDto/{materialId}")
    public MesMaterialBatchesDto getInfoDto(@PathVariable("materialId") Integer materialId)
    {
        MesMaterialBatches mesMaterialBatches=mesMaterialBatchesService.selectMesMaterialBatchesByMaterialId(materialId);
       MesMaterialBatchesDto mesMaterialBatchesDto=new MesMaterialBatchesDto();
        // 复制属性
        BeanUtils.copyProperties(mesMaterialBatches, mesMaterialBatchesDto);
        return mesMaterialBatchesDto;
    }

    /**
     * 新增MES系统物料批次，用于管理物料批次全流程信息
     */
    @RequiresPermissions("material-service:batches:add")
    @Log(title = "MES系统物料批次，用于管理物料批次全流程信息", businessType = BusinessType.INSERT)
    @PostMapping("/batches")
    public AjaxResult add(@RequestBody MesMaterialBatches mesMaterialBatches)
    {
        return toAjax(mesMaterialBatchesService.insertMesMaterialBatches(mesMaterialBatches));
    }

    /**
     * 修改MES系统物料批次，用于管理物料批次全流程信息
     */
    @RequiresPermissions("material-service:batches:edit")
    @Log(title = "MES系统物料批次，用于管理物料批次全流程信息", businessType = BusinessType.UPDATE)
    @PutMapping("/batches")
    public AjaxResult edit(@RequestBody MesMaterialBatches mesMaterialBatches)
    {
        return toAjax(mesMaterialBatchesService.updateMesMaterialBatches(mesMaterialBatches));
    }

    @PutMapping("/batchesDtos")
    public void editDtos(@RequestBody MesMaterialBatchesDto mesMaterialBatchesDto)
    {
        mesMaterialBatchesService.updateMesMaterialBatchesDto(mesMaterialBatchesDto);
    }

    @PutMapping("/batchesDto")
    public void editByWorkOrder(@RequestBody MesMaterialBatchesDto mesMaterialBatchesDto)
    {
       mesMaterialBatchesService.updateMesMaterialBatchesByOrder(mesMaterialBatchesDto);
    }
    /**
     * 删除MES系统物料批次，用于管理物料批次全流程信息
     */
    @RequiresPermissions("material-service:inventory:batches:remove")
    @Log(title = "MES系统物料批次，用于管理物料批次全流程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/batches/{batchIds}")
    public AjaxResult remove(@PathVariable Long[] batchIds)
    {
        return toAjax(mesMaterialBatchesService.deleteMesMaterialBatchesByBatchIds(batchIds));
    }
}
